$(document).ready(function() {

	/* table append rows */
	if ( $(".appendTable").length ) fe_appendTable();

	/* radio input lock */
	if ( $(".radioArea .member").length ) fe_radioArea();

	/* select input toggle */
	if ( $(".selectArea").length ) fe_selectToggle();

	/* layer */
	if ( $(".layOpen").length ) fe_layPop();
	if ( $(".layOpen2").length ) fe_layPop2();
	if ( $(".layOpenDel").length ) fe_layPopDel();
	if ( $(".faceOpen").length ) fe_facePop();
	if ( $(".movieOpen").length ) fe_moviePop();

	/* whole */
	if ( $("#gnb .wholeOpen").length ) fe_whole();

	/* gnb */
	if ( $("#gnb .menu > li > a").length ) fe_gnbSub();

	/* textarea form */
	if ( $(".btnCmt button").length ) fe_textareaForm();

	/* tab */
	if ( $(".copyList li a").length ) fe_copyrightTab();
	if ( $(".noticeHeader li a").length ) fe_noticeTab();

	/* gnb */
	$("#gnb > .menu > li > a").click(function(){
		ChkGnb();
		return false;
	});

});
$(window).resize(function(){
	/* gnb */
	ChkGnb();
});

$(window).load(function(){

	/* layer height */
	if ( $(".layer").length ) fe_maskHeight();

	/* layer delete height */
	if ( $(".layerDel").length ) fe_maskDelHeight();

	/* faceLayer height */
	if ( $(".faceLayer").length ) fe_faceMaskHeight();

	/* faceLayer height */
	if ( $(".movieLayer").length ) fe_movieMaskHeight();

});

/* layer height */
function fe_maskHeight(){
	$(".layer").height( $("#wrap").height() + "px");
}

function fe_maskDelHeight(){
	$(".layerDel").height( $("#wrap").height() + "px");
}

function fe_faceMaskHeight(){
	$(".faceLayer").height( $("#wrap").height() + "px");
}

function fe_movieMaskHeight(){
	$(".movieLayer").height( $("#wrap").height() + "px");
}

/* table append rows */
function fe_appendTable(){
	$(".appendRow").click(function(){
		var tr_obj    = document.getElementById("lineAppend");
		var clone_obj = tr_obj.cloneNode(true)
		tr_obj.parentNode.appendChild(clone_obj);
	});

	$(".deleteRow").click(function(){
		var tr_obj    = document.getElementById("lineAppend");
		var tbody_obj = tr_obj.parentNode;
		var tbl_obj   = tbody_obj.parentNode;
		var tbl_cnt   = tbl_obj.rows.length;tbl_cnt
		if(tbl_cnt < 3) {
			alert("삭제할 수 없습니다.");
		return;
		}
		tr_obj.parentNode.removeChild(tr_obj);
	});
}

/* radio input lock */
function fe_radioArea(){
	$(".radioArea .member").click(function(){
		var radioVal = $(this).val();
		if(radioVal == 1){
			$(".team").addClass("lock");
			$('.team input[type="text"]').attr("readonly","readonly");
			$('.team input[type="text"]').attr("value","");
		}else{
			$(".team").removeClass("lock");
			$('.team input[type="text"]').removeAttr("readonly");
		}
	});
}

/* select input toggle */
function fe_selectToggle(){
	$(".selectArea p").click(function(){
		$(this).next("ul").toggle();
		$(this).parent().toggleClass("zIndex");
		return false;
	});
	$(".selectArea ul li a").click(function(){
		$(this).parent().parent("ul").toggle();
	});
	$(".selectArea").mouseleave(function(){
		$(this).find("ul").hide();
	});
}

/* layer */
function fe_layPop(){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		$(".layer").css({"width":maskWidth,"height":maskHeight}).show();
		var layerHeight = $(".layPop > div").height();
		var layerWidth = $(".layPop > div").width();
		var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
		var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
		$(".layer .mask").css({"display" : "block"});
		$(".layPop").css({"top": top + "px", "left" : left + "px"}).show();
	}
	$(".layOpen").click(function(e){
		e.preventDefault();
		wrapWindowByMask();
	});
	$(".layer .mask, .layer .btnClose > button").click(function (e) {
		e.preventDefault();
		$(".layer").hide();
		try{
			var _ybPlayer    = document.getElementById("ybPlayer");
			_ybPlayer.innerHTML = "";
		}catch(e){
		}
	});
}

/* layer2 */
function fe_layPop2(){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$(".layer2").css({"width":maskWidth,"height":maskHeight}).show();
		var layerHeight = $(".layPop2 > div").height();
		var layerWidth = $(".layPop2 > div").width();
		var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
		var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
		$(".layer2 .mask").css({"display" : "block"});
		$(".layPop2").css({"top": top + "px", "left" : left + "px"}).show();
	}
	$(".layOpen2").click(function(e){
		$(".layer").hide();
		e.preventDefault();
		wrapWindowByMask();
	});
	$(".layer2 .mask, .layer2 .btnClose > button, .layer2 .modalClose").click(function (e) {
		e.preventDefault();
		$(".layer2").hide();
	});
}

/* layer delete */
function fe_layPopDel(){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$(".layerDel").css({"width":maskWidth,"height":maskHeight}).show();
		var layerHeight = $(".layPopDel > div").height();
		var layerWidth = $(".layPopDel > div").width();
		var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
		var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
		$(".layerDel .mask").css({"display" : "block"});
		$(".layPopDel").css({"top": top + "px", "left" : left + "px"}).show();
	}
	$(".layOpenDel").click(function(e){
		e.preventDefault();
		wrapWindowByMask();
	});
	$(".layerDel .mask, .layerDel .btnClose > button").click(function (e) {
		e.preventDefault();
		$(".layerDel").hide();
	});
}

/* facebook layer */
function fe_facePop(){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$(".faceLayer").css({"width":maskWidth,"height":maskHeight}).show();
		var layerHeight = $(".faceView").height();
		var layerWidth = $(".faceView").width();
		var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
		var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
		$(".faceLayer .mask").css({"display" : "block"});
		$(".faceLayPop").css({"top": top + "px", "left" : left + "px"}).show();

	}
	$(".faceOpen").click(function(e){
		e.preventDefault();
		wrapWindowByMask();
	});
	$(".faceLayer .mask, .faceLayer .btnClose > button").click(function (e) {
		e.preventDefault();
		$(".faceLayer").hide();
	});
}

/* download layer */
function fe_downPop(){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$(".layer").css({"width":maskWidth,"height":maskHeight}).show();
		var layerHeight = $(".downLayer").outerHeight();
		var layerWidth = $(".downLayer").outerWidth();
		var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
		var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
		$(".layer .mask").show();
		$(".layPop").css({"top": top + "px", "left" : left + "px"}).show();

	}

	$(".layer .mask, .layer .btnClose2 > button").click(function (e) {
		e.preventDefault();
		$(".layer").hide();
	});

	wrapWindowByMask();
	$('.scrollAreaY').myScroll({direction : 'y', wheel : 'true'});//customer scroll
}

/* movie layer */
function fe_moviePop(){
	$(".movieOpen").attr("target", "_blank");
	var winW = $(window).width();
	if (navigator.userAgent.match(/Mobile/i) !== null || winW < 975) {
		$(".movieOpen").unbind("click");
	}
	else{
		function wrapWindowByMask(){
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
			$(".movieLayer").css({"width":maskWidth,"height":maskHeight}).show();
			var layerHeight = $(".movieView").outerHeight();
			var layerWidth = $(".movieView").outerWidth();
			var left = ( $(window).scrollLeft() + ($(window).width() - layerWidth) / 2 );
			var top = ( $(window).scrollTop() + ($(window).height() - layerHeight) / 2 );
			$(".movieLayer .mask").show();
			$(".movieLayPop").css({"top": top + "px", "left" : left + "px"}).show();
			projekktor('#mainPlayer', {	volume: 0.8, enableFullscreen: false, playerFlashMP4: '/img/usr/video/jarisplayer.swf' }, function(player){
				$('.mask, .btnClose > button').click(function(){player.setStop(); return false;})
			});
		}
		$(".movieOpen").bind("click", function(e){
			e.preventDefault();
			wrapWindowByMask();
		});
		$(".movieLayer .mask, .movieLayer .btnClose > button").click(function (e) {
			e.preventDefault();
			$(".movieLayer").hide();
		});
	}
}

/* map layer */
function fe_mapPop(imgNm){
	function wrapWindowByMask(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		var layerWidth = $(".layPop > div").width();
		$(".layer").css({"width":maskWidth,"height":maskHeight}).show();
		$(".layer .mask").show();
		$(".layPop").css({"top": 180 +"px", "margin-left" : "-" + layerWidth/2 + "px"}).show();
	}

	$(".layer .mask, .layer .btnClose > button, .nationalAct dd a").click(function(){
		$(".layer").hide();
	});

	$(".continMap img").attr("src", "/img/usr/srt/continVolun_map_" + imgNm + ".gif");
	$(".mapLayerCon").hide();
	if(imgNm == "01"){//europe
		$("#popEurope").show();
	}else if(imgNm == "02"){//middle east africa
		$("#popMidEastAfrica").show();
	}else if(imgNm == "03"){//asia pacific
		$("#popAsiaPacific").show();
	}else if(imgNm == "04"){//america
		$("#popAmerica").show();
	}
	wrapWindowByMask();
	$('.scrollAreaY').myScroll({direction : 'y', wheel : 'true'});//customer scroll
}

/* whole */
function fe_whole(){
	var state = 1;

	$("#gnb .wholeOpen").click(function(){
		$(".whole").toggle();
		$("#gnb .menu").removeClass("subArea");
		$("#gnb .menu > li").removeClass("atv");

		if(state == 0){
			state = 1;
			$("#gnb .wholeOpen > img").attr({ src: "/img/usr/btn/btn_gnb_total.gif", alt: "전체보기"});
		}else{
			state = 0;
			$("#gnb .wholeOpen > img").attr({ src: "/img/usr/btn/btn_gnb_total_atv.gif", alt: "전체보기 메뉴 닫기"});
		}
	});
}

/* gnb */
function fe_gnbSub(){
	$("#gnb .menu > li > a").click(function(){
		$(this).parent().parent().addClass("subArea");
		$(this).parent().addClass("atv").siblings().removeClass("atv");
		$(".whole").css("display","none");
	});

	$(".subMenuClose").click(function(){
		$("#gnb .menu > li").removeClass("atv");
		$("#gnb .menu").removeClass("subArea");
	});
}

/* textarea form */
function fe_textareaForm(){
	$(".btnCmt button").click(function (){
		$(this).parent().next(".commentFrm").toggle();
	});
	$("#CmtText").click(function(){
		$(this).text($(this).text() == "답글달기" ? "답글취소" : "답글달기");
	});
}
/* copyright tab */
function fe_copyrightTab(){
	$(".copyright .copyrightCon:first").css("display","block");
	$(".copyList li:first").addClass("atv");
	$(".copyList li a").click(function(){
		$(this).parent().addClass("atv").siblings().removeClass("atv")
		$($(this).attr("href")).css("display","block").siblings(".copyrightCon").css("display","none");
		return false;
	});
}

/* main notice tab */
function fe_noticeTab(){
	$(".mainNotice .noticeList:first").css("display","block");
	$(".noticeHeader li:first").addClass("atv");
	$(".noticeHeader li a").click(function(){
		$(this).parent().addClass("atv").siblings().removeClass("atv")
		$($(this).attr("href")).css("display","block").siblings(".noticeList").css("display","none");
		return false;
	});
}

/* main : check element for scroll event */
function ChkScroll(){
	var dragger = $(".dragger");
	if($(dragger).is(":visible")){
		$('.scrollAreaY').myScroll({direction : 'y', wheel : 'true'});
		$('.scrollAreaX').myScroll({direction : 'x', wheel : 'true'});
	}
}

/* main : check mobile for gallery link */
function ChkGalleryLink(){
	if (navigator.userAgent.match(/Mobile/i) == null || navigator.userAgent.match(/iPad/i) !== null) {
		return false;
	}else{
		$(".layOpen").unbind("click");
	}
}

/* navi : gnb respond ctrl */
function ChkGnb(){
	var winW = $(window).width();
	var menuH = 0;
	if(winW > 720 && winW < 980){
		if($("#gnb > .menu > li").hasClass("atv")){
			menuH = $("#gnb > .menu > li.atv").find(".subMenu").outerHeight() + $("#gnb > .menu > li.atv").find("a:eq(0)").outerHeight();
		}else{
			menuH = 40;
		}
	}else if(winW <= 720){
		var H = [];
		$("#gnb > .menu > li").each(function(i){
			H[i] = $(this).outerHeight();
			menuH = menuH + H[i];
		});
	}else{
		menuH = 28;
	}
	$(".menu").css("height", menuH + "px");
}

/* hide layer */
function HideLayer(){
	$(".mask").click();
}
